package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageBox;
import com.moleq.models.GlobalInfo;
import com.moleq.models.PosSetting;
import com.moleq.models.TareSetting;

public class TareSettingService {
	private DatabaseHelper DBHelper;
	private String errorString;
	private Context mContext;

	public TareSettingService() {

	}

	public TareSettingService(Context context) {
//		this.DBHelper = new DatabaseHelper(context);
		mContext = context;
		this.DBHelper = new DatabaseHelper(GlobalInfo.getInstance().ShareContext);
	}
	
	public List<TareSetting> FindAll(){
		List<PosSetting> posSetting = new ArrayList<PosSetting>();
		PosSettingService posService = ServiceManager.getInstance().mPosSettingService;
		posSetting = posService.getAllPosSetting();
		
		List<TareSetting> obj = new ArrayList<TareSetting>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from t_tare_setting",null);
//		Cursor cursor = DBHelper.rawQuery("select * from t_tare_setting",null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			int ta_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TareSetting.TA_NO));
			String ta_desc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TareSetting.TA_DESC));
			int ta_value  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TareSetting.TA_VALUE));
			String create_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TareSetting.CREATE_TIME));
			String update_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TareSetting.UPDATE_TIME));
			
			TareSetting ts = new TareSetting(ta_no,ta_desc,ta_value,1,create_datetime,update_datetime);
			ts.setType("0");
			double tareRate = posSetting.get(0).getPs_tare_rate();
			ts.setTotalTare(ta_value * tareRate);
			
			obj.add(ts);
		}
		cursor.close();
		return obj;
	}
	
	public List<TareSetting> Find(String upc){
		List<TareSetting> obj = new ArrayList<TareSetting>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from t_item_tare_setting left join t_tare_setting on ta_no = it_tare_no where F01  = '"+upc+"'",null);
//		Cursor cursor = DBHelper.rawQuery("select * from t_item_tare_setting left join t_tare_setting on ta_no = it_tare_no where F01  = '"+upc+"'",null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return null;
		}
		while(cursor.moveToNext()){
			int ta_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TareSetting.TA_NO));
			String ta_desc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TareSetting.TA_DESC));
			int ta_value  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TareSetting.TA_VALUE));
			String create_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TareSetting.CREATE_TIME));
			String update_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TareSetting.UPDATE_TIME));
			
			obj.add(new TareSetting(ta_no,ta_desc,ta_value,1,create_datetime,update_datetime));
		}
		cursor.close();
		return obj;
	
	}
	
	public int HaveTare(String upc){
		int havetare = 0;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select count(it_no) from t_item_tare_setting where F01  = '"+upc+"'",null);
//		Cursor cursor = DBHelper.rawQuery("select count(it_no) from t_item_tare_setting where F01  = '"+upc+"'",null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return havetare;
		}
		if(cursor.moveToNext()){
			havetare = cursor.getInt(0);
		}
		cursor.close();
		return havetare;
	}
	
	public boolean ItemHaveTare(int tareNo){
		boolean result = false;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select it_no from t_item_tare_setting where it_tare_no = " + tareNo, null);
//		Cursor cursor = DBHelper.rawQuery("select it_no from t_item_tare_setting where it_tare_no = " + tareNo, null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return result;
		}
		if (cursor.moveToNext()) {
			result = true;
		}
		cursor.close();
		return result;
	}
	
	public boolean updateSetTareNo(int tare_no){
		boolean check = true;
		errorString = DBHelper.execSQL("Update "+ DatabaseSchema.TareSetting.SCHEMA_NAME +" set ta_no = ta_no - 1 where ta_no > " + tare_no);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean insertTareSetting(TareSetting obj){
		boolean check = true;
		String sql,desc;
		double value;
		desc = obj.getTa_desc();
		value = obj.getTa_value();
		sql = "Insert into "+ DatabaseSchema.TareSetting.SCHEMA_NAME +" values";
		sql += "(null,'"+ desc +"',"+ value +",strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'))";
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean updateTareSetting(TareSetting obj){
		boolean check = true;
		String sql,desc;
		double value;
		int no;
		no = obj.getTa_no();
		desc = obj.getTa_desc();
		value = obj.getTa_value();
		sql = "UPDATE "+ DatabaseSchema.TareSetting.SCHEMA_NAME +" set ";
		sql += "ta_desc = '"+ desc +"',ta_value = "+ value +",update_datetime = strftime('%Y%m%d%H%M%S','now')" +
				" where ta_no = "+ no;
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean deleteTareSetting(int no){
		boolean check = true;
		String sql = "delete from "+ DatabaseSchema.TareSetting.SCHEMA_NAME +" where ta_no = "+ no;
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public int getMaxTareNo(){
		int order = 0;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select Max(ta_no) from " + DatabaseSchema.TareSetting.SCHEMA_NAME, null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return order;
		}
		if (cursor.moveToNext()) {
			order = cursor.getInt(0);
		}
		cursor.close();
		return order;
	}
}
